package com.android.graffiti;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.RadioButton;

import com.common.base.ComponentContext;
import com.photoeditor.R;


/**
 * 表示笔触宽度的自定义单选按钮
 * @see RadioButton
 */
public class BrushWidthRadioButton extends RadioButton {
//    private Rect mWholeRect;
    private Paint mCirclePaint;
    private Paint mStrokePaint;
    private Paint mBorderPaint;
    
    private static Paint sMaskPaintChecked;

    static {
        if(sMaskPaintChecked == null) {
            sMaskPaintChecked = new Paint();
            sMaskPaintChecked.setColor(0xFF000000);
            sMaskPaintChecked.setStyle(Paint.Style.FILL);
            sMaskPaintChecked.setAntiAlias(true);
            sMaskPaintChecked.setAlpha(180);
        }
    }
    
    private float mBrushWidth = 10f;
    
    /**
     * 获取要显示的笔触大小
     * @return 要显示的笔触大小(px)
     */
    public float getBrushWidth() {
        return mBrushWidth;
    }
    
    /**
     * 设置要显示的笔触大小
     * @param brushWidth 要显示的笔触大小(px)
     */
    public void setBrushWidth(float brushWidth) {
        this.mBrushWidth = brushWidth;
    }

    
    public BrushWidthRadioButton(Context context) {
        super(context);
    }
    
    public BrushWidthRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeWithAttrs(context, attrs);
    }
    
    public BrushWidthRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initializeWithAttrs(context, attrs);
    }

    
    /**
     * 使用XML属性进行初始化，<b>初始值请在此处修改</b>
     * @param context 上下文对象
     * @param attrs 属性集
     */
    private void initializeWithAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, 
                R.styleable.BrushWidthRadioButton);
        
        mBrushWidth = typedArray.getDimension(R.styleable.BrushWidthRadioButton_brushWidth, 10f);
        float strokeWidth = typedArray.getDimension(R.styleable.BrushWidthRadioButton_strokeWidth, ColorRadioButton.STROKE_PAINT_WIDTH);
        int circleColor = typedArray.getColor(R.styleable.BrushWidthRadioButton_circleColor, 0xffff5722);
        int strokeColor = typedArray.getColor(R.styleable.BrushWidthRadioButton_strokeColor, ComponentContext.getContext().getResources().getColor(R.color.accent_color));
        
        mCirclePaint = new Paint();
        mCirclePaint.setColor(circleColor);
        mCirclePaint.setStyle(Paint.Style.FILL);
        mCirclePaint.setAntiAlias(true);
        
        mStrokePaint = new Paint();
        mStrokePaint.setColor(strokeColor);
        mStrokePaint.setStrokeJoin(Paint.Join.ROUND);
        mStrokePaint.setStrokeWidth(strokeWidth);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setAntiAlias(true);

        mBorderPaint = new Paint();
        mBorderPaint.setColor(0xff262626);
        mBorderPaint.setStrokeJoin(Paint.Join.ROUND);
        mBorderPaint.setStrokeWidth(strokeWidth);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setAntiAlias(true);
        
        typedArray.recycle();
    }

    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);
        invalidate();
    }
    

    @Override
    protected void onDraw(Canvas canvas) {
        if (isChecked()) {
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBrushWidth / 2 + 1, mStrokePaint);
        } else {
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBrushWidth / 2 + 1, mBorderPaint);
        }
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBrushWidth / 2, mCirclePaint);
    }
}
